package com.wuad.platformwuad.module.study.stringMethods;

import lombok.extern.slf4j.Slf4j;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 驼峰与下划线互转
 *
 * @author ：Wuad
 * @date ：Created in 2021/3/27 8:38
 * @description :
 */
@Slf4j
public class LineOrHumpTransition {
    private static Pattern linePattern = Pattern.compile("_(\\w)");



    public static void main(String[] args) {
        String ss = camel4underline("coustomerName");
        System.out.println(ss);

        String vv = LineToHump("coustomer_name");
        System.out.println(vv);
    }

    // 将大写转小写并在前加上下划线

    /**
     * 驼峰转下划线
     * @param param
     * @return
     */
    public static String camel4underline(String param){
        Pattern p = Pattern.compile("[A-Z]");
        if(param==null ||param.equals("")){
            return "";
        }
        StringBuilder builder=new StringBuilder(param);
        Matcher mc=p.matcher(param);
        int i=0;
        while(mc.find()){
            builder.replace(mc.start()+i, mc.end()+i, "_"+mc.group().toLowerCase());
            i++;
        }

        if('_' == builder.charAt(0)){
            builder.deleteCharAt(0);
        }
        return builder.toString();
    }

    /**
     * 下划线转驼峰
     *
     * @param fieldName 带下划线字段
     * @return 驼峰字段
     */
    public static String LineToHump(String fieldName) {
        fieldName = fieldName.toLowerCase();
        Matcher matcher = linePattern.matcher(fieldName);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * 将字符串转大小写
     */
    public static class up_lowCase {
        public static void main(String[] args) {
            String s1 = "SubBranchLeader";
            log.info("将字符串转小写：{}", s1.toLowerCase());
            log.info("将字符串转大写：{}", s1.toUpperCase());
        }
    }

}
